package pl.edu.kassiopeia.repository.util.json;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.xml.XMLSerializer;

import org.xml.sax.SAXException;

import pl.edu.kassiopeia.repository.model.Plugin;

public class JsonConverter {

	public JsonConverter(){
		
	}
	
	public String convert(List<Plugin> list){
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] {"jarFile"});

		return JSONSerializer.toJSON(list, jsonConfig).toString();
	}
	
	public String convert(String pluginDescriptor) throws SAXException, IOException, ParserConfigurationException{
		XMLSerializer xmlSerializer = new XMLSerializer();
		JSON json = xmlSerializer.read(pluginDescriptor);
		return json.toString(2);
	}
	
	public String convert(Plugin plugin){
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] {"jarFile"});

		return JSONSerializer.toJSON(plugin, jsonConfig).toString();
	}
}
